package org.itboys.mobile.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayLouKa;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 集楼_楼卡_web_service
 */

@Service
public class SundayLouKaService extends BaseMongoService<SundayLouKa> {
    @Autowired
    private SundayUploadService uploadFileService;
    @Autowired
    private SundayActivityRelationService relationService;
    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;

    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }

    @Override
    protected Class<SundayLouKa> getEntityClass() {
        return SundayLouKa.class;
    }

    /**
     * 新增或者修改楼卡信息
     * @param louka            楼卡实体
     * @param imageFile        地图图片
     * @param defaultImageFile 默认楼卡图片
     * @throws Exception
     */
    public void saveBuilding(SundayLouKa louka, MultipartFile imageFile, MultipartFile defaultImageFile) throws Exception {
        //保存地图
        Map<String, String> map = uploadFileService.uploadSingle(imageFile);
        if (StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))) {
            louka.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        //保存默认图片
        Map<String, String> map2 = uploadFileService.uploadSingle(defaultImageFile);
        if (StringUtils.isNotEmpty(map2.get(SundayUploadService.UPLOAD_HALF_PATH))) {
            louka.setDefaultImage(map2.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        //保存
        if (LongUtil.isNotZreo(louka.getId())) {
            super.updateExceptEmpty(louka.getId(), louka);
        } else {
            super.save(louka);
        }
    }

    /**
     * 获取楼卡信息
     *
     * @param request
     * @return
     */
    public PageResult<SundayLouKa> select(HttpServletRequest request) {
        Map<String, String> containsparam = ParamUtil.packageMongoVagueParam(request, "name");
        Map<String, Object> param = new HashMap<>();
        param.put("isDeleted", SundayCommonConstants.NO);
        PageResult<SundayLouKa> result = super.containpageQuery(request, containsparam, param);
        return result;
    }

    /**
     * 单个查询楼卡
     *
     * @param id
     * @return 单个楼卡对象
     */
    public SundayLouKa findOne(Long id) {
        SundayLouKa louka = new SundayLouKa();

        if (LongUtil.isNotZreo(id)) {
            louka = super.getById(id);
        } else {
            louka = new SundayLouKa();
        }
        return louka;
    }


    public void delete(Long loukaId) {
        super.delete(loukaId);
        //同时删除相关的楼卡
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("otherId",loukaId);
        param.put("type",2);
        relationService.delete(param);
    }

}
